package com.ruoyi.carowner.service.impl;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.carowner.domain.CarownerPaymentRecords;
import com.ruoyi.carowner.domain.CarownerWalletBankcard;
import com.ruoyi.carowner.service.ICarownerPaymentRecordsService;
import com.ruoyi.carowner.service.ICarownerWalletBankcardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.carowner.mapper.CarownerWalletMapper;
import com.ruoyi.carowner.domain.CarownerWallet;
import com.ruoyi.carowner.service.ICarownerWalletService;

import static java.time.LocalDate.now;

/**
 * 车主钱包管理Service业务层处理
 *
 * @author aa
 * @date 2024-08-13
 */
@Service
public class CarownerWalletServiceImpl implements ICarownerWalletService {
    @Autowired
    private CarownerWalletMapper carownerWalletMapper;

    @Autowired
    private ICarownerWalletBankcardService bankcardService;

    @Autowired
    private ICarownerPaymentRecordsService paymentRecordsService;

    /**
     * 查询车主钱包管理
     *
     * @param id 车主钱包管理主键
     * @return 车主钱包管理
     */
    @Override
    public CarownerWallet selectCarownerWalletById(Integer id) {
        return carownerWalletMapper.selectCarownerWalletById(id);
    }

    /**
     * 查询车主钱包管理列表
     *
     * @param carownerWallet 车主钱包管理
     * @return 车主钱包管理
     */
    @Override
    public List<CarownerWallet> selectCarownerWalletList(CarownerWallet carownerWallet) {
        return carownerWalletMapper.selectCarownerWalletList(carownerWallet);
    }

    /**
     * 新增车主钱包管理
     *
     * @param carownerWallet 车主钱包管理
     * @return 结果
     */
    @Override
    public int insertCarownerWallet(CarownerWallet carownerWallet) {
        return carownerWalletMapper.insertCarownerWallet(carownerWallet);
    }

    @Override
    public CarownerWallet selectWalletListByUserid(Integer user_id) {
        return carownerWalletMapper.selectWalletListByUserid(user_id);
    }

    /**
     * 修改车主钱包管理
     *
     * @param carownerWallet 车主钱包管理
     * @return 结果
     */
    @Override
    public int updateCarownerWallet(CarownerWallet carownerWallet) {
        LocalDate localDate = LocalDate.now();
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        Date date = Date.from(zonedDateTime.toInstant());
        if (carownerWallet.getBalance() == null || carownerWallet.getUserId() == null) {
            return 0;
        }
        CarownerPaymentRecords paymentRecords = new CarownerPaymentRecords();
        paymentRecords.setPaymoney(carownerWallet.getPaymoney());
        if(carownerWallet.getTransstatus() == 0 || carownerWallet.getTransstatus() == null) {
            paymentRecords.setTransstatus(0);
        }else{
            paymentRecords.setTransstatus(1);
        }
        paymentRecords.setPaytime(date);
        paymentRecords.setUserId(carownerWallet.getUserId());
        paymentRecords.setOrderId(UUID.randomUUID().toString());
        paymentRecords.setPaystatus("0");
        paymentRecords.setPaycateId(carownerWallet.getPaytype());
        paymentRecordsService.insertCarownerPaymentRecords(paymentRecords);


        CarownerWalletBankcard bankcard = bankcardService.selectCarownerWalletBankcardById(carownerWallet.getUserId());
        bankcard.setBalance(bankcard.getBalance() - carownerWallet.getPaymoney());

        bankcardService.updateCarownerWalletBankcard(bankcard);

        // 更新钱包信息
        int rowsAffected = carownerWalletMapper.updateCarownerWallet(carownerWallet);
        if (rowsAffected == 0) {
            throw new RuntimeException("更新失败");
        }
        return rowsAffected;
    }


    /**
     * 批量删除车主钱包管理
     *
     * @param ids 需要删除的车主钱包管理主键
     * @return 结果
     */
    @Override
    public int deleteCarownerWalletByIds(Integer[] ids) {
        return carownerWalletMapper.deleteCarownerWalletByIds(ids);
    }

    /**
     * 删除车主钱包管理信息
     *
     * @param id 车主钱包管理主键
     * @return 结果
     */
    @Override
    public int deleteCarownerWalletById(Integer id) {
        return carownerWalletMapper.deleteCarownerWalletById(id);
    }
}
